﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HumanResourceManagementSystem.BusinessLogicLayer;
using System.Xml;
using System.Configuration;

namespace HumanResourceManagementSystem.PresentationLayer
{
    public partial class frmPeopleManagement : Form
    {
        public frmPeopleManagement()
        {
            InitializeComponent();
        }

        private void chkBoxSearch_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBoxSearch.Checked)
            {
                txtName.Enabled = true;
                btnSearch.Enabled = true;
                dataGridView1.Enabled = true;
            }
            else 
            {
                txtName.Enabled = false;
                btnSearch.Enabled = false;
                dataGridView1.Enabled = false;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = EmployeeManagement.getEmployeeByName(txtName.Text);
            dataGridView1.AutoResizeColumns();
            txtEmployeeID.DataBindings.Clear();
            txtEmployeeID.DataBindings.Add("Text", dataGridView1.DataSource, "EmpID");
            txtName.DataBindings.Clear();
            txtName.DataBindings.Add("Text", dataGridView1.DataSource, "EmpName");
            cboxProject1.DataBindings.Clear();
            cboxProject1.DataBindings.Add("Text", dataGridView1.DataSource, "ProjName");
            txtGoal.DataBindings.Clear();
            txtGoal.DataBindings.Add("Text", dataGridView1.DataSource, "Goal");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabEmployee)
            {
                
            }
            else if (tabControl1.SelectedTab == tabResource)
            {

            }
            else if (tabControl1.SelectedTab == tabProject)
            {
                if (ProjectManagement.writeToXml(cboxProject2.SelectedValue.ToString(), cboxProject2.Text, startDate.Value, endDate.Value, cboxClient.SelectedValue.ToString()))
                    MessageBox.Show("Write to Projects.xml successfully!", "Notice");
                else MessageBox.Show("There's an error Writing to Projects.xml!", "Notice");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabEmployee)
            {
                int projID = Convert.ToInt32(cboxProject1.SelectedValue);
                string goal = txtGoal.Text;
                if (!chkBoxRange.Checked)
                {
                    if (EmployeeManagement.empAssign(txtEmployeeID.Text, projID, goal))
                        MessageBox.Show("Employee assigned successfully!", "Notice");
                    else MessageBox.Show("Employee not assigned! Please check your information!", "Notice");
                }
                else
                {
                    List<int> empIDList = new List<int>();
                    foreach (DataGridViewRow drv in dataGridView1.SelectedRows)
                    {
                        empIDList.Add(Convert.ToInt32(drv.Cells[0].Value));
                    }
                    int lines = EmployeeManagement.empAssign(empIDList, projID, goal);
                    if (lines > 0) MessageBox.Show(lines + " employees assigned successfully!", "Notice");
                    else MessageBox.Show("Employees not assigned! Please check your information!", "Notice");
                }
            }
            else if (tabControl1.SelectedTab == tabResource)
            {
                if (ResourceManagement.resAssign(cboxResource.SelectedValue.ToString(), Convert.ToInt32(cboxEmployee.SelectedValue), DateTime.Now, freeDate.Value))
                    MessageBox.Show("Resource assigned successfully!", "Notice");
                else MessageBox.Show("Resource not assigned! Please check your information!", "Notice");
            }
            else if (tabControl1.SelectedTab == tabProject)
            {

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Are you sure to Exit?", "Confirm", MessageBoxButtons.YesNo);
            if (dr.Equals(DialogResult.Yes)) this.Close();
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (tabControl1.SelectedTab == tabEmployee)
            {
                DataTable dt = ProjectManagement.getProjets();
                cboxProject1.DataSource = dt;
                cboxProject1.DisplayMember = "ProjName";
                cboxProject1.ValueMember = "ProjID";
            }
            else if (tabControl1.SelectedTab == tabResource)
            {
                DataTable dt1 = ResourceManagement.getResources();
                cboxResource.DataSource = dt1;
                cboxResource.DisplayMember = "ResName";
                cboxResource.ValueMember = "ResID";

                DataTable dt2 = EmployeeManagement.getEmployeeByName("");
                cboxEmployee.DataSource = dt2;
                cboxEmployee.DisplayMember = "EmpName";
                cboxEmployee.ValueMember = "EmpID";
            }
            else if (tabControl1.SelectedTab == tabProject)
            {
                DataTable dt1 = ProjectManagement.getProjets();
                cboxProject2.DataSource = dt1;
                cboxProject2.DisplayMember = "ProjName";
                cboxProject2.ValueMember = "ProjID";

                DataTable dt2 = ClientManagement.getClients();
                cboxClient.DataSource = dt2;
                cboxClient.DisplayMember = "ClientName";
                cboxClient.ValueMember = "ClientID";
            }
        }

        private void frmPeopleManagement_Load(object sender, EventArgs e)
        {
            tabControl1.SelectTab(1);
            tabControl1.SelectTab(0);
        }

        private void txtEmployeeID_Validating(object sender, CancelEventArgs e)
        {
            if (txtEmployeeID.Text != "")
            {
                if (!EmployeeManagement.empoyeeExisted(txtEmployeeID.Text))
                {
                    MessageBox.Show("There is no employee having this ID!", "Notice");
                    txtName.Text = "";
                    cboxProject1.Text = "";
                    txtGoal.Text = "";
                    dataGridView1.DataSource = null;
                    //txtEmployeeID.Focus();
                }
                else
                {
                    dataGridView1.DataSource = EmployeeManagement.getEmployeeByID(Convert.ToInt32(txtEmployeeID.Text));
                    dataGridView1.AutoResizeColumns();
                    txtEmployeeID.DataBindings.Clear();
                    txtEmployeeID.DataBindings.Add("Text", dataGridView1.DataSource, "EmpID");
                    txtName.DataBindings.Clear();
                    txtName.DataBindings.Add("Text", dataGridView1.DataSource, "EmpName");
                    cboxProject1.DataBindings.Clear();
                    cboxProject1.DataBindings.Add("Text", dataGridView1.DataSource, "ProjName");
                    txtGoal.DataBindings.Clear();
                    txtGoal.DataBindings.Add("Text", dataGridView1.DataSource, "Goal");
                }
            }
        }

        private void chkBoxRange_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBoxRange.Checked) chkBoxSearch.Checked = true;
        }
    }
}
